<?php

declare (strict_types=1);

namespace app\beautiful\controller\miniapp;
use app\beautiful\model\project\OnceCardModel;
use app\beautiful\model\setting\SettingBalanceModel;
use app\beautiful\model\order\BalanceOrderModel;
use app\beautiful\model\CouponModel;
use app\beautiful\helper\payment\PaymentBalanceHelper;
use app\beautiful\model\order\BuyOrderModel;
use app\beautiful\helper\payment\PaymentBuyHelper;
use app\beautiful\model\order\GoodsOrderModel;
use app\beautiful\helper\payment\PaymentGoodsOrderHelper;
use app\beautiful\model\order\StoreOrderModel;
use app\beautiful\helper\payment\PaymentStoreOrderHelper;
use app\beautiful\helper\payment\PaymentPintuanOrderHelper;
use app\beautiful\model\order\PinTuanOrderModel;
use app\beautiful\model\order\SnapUpOrderModel;
use app\beautiful\helper\payment\PaymentSnapupHelper;
use app\beautiful\helper\payment\PaymentOncecardHelper;
use app\beautiful\model\order\OnceCardOrderModel;
class Payment extends \app\BaseController{
    
    //下充值单
    public function getBalancePayment(){
        $selectIndex = $this->request->param('selectIndex/d');
        $setting_balance = SettingBalanceModel::where([['shop_id','=',$this->request->shopId]])->find();
        if(null === $setting_balance){
            return $this->error('暂未配置充值套餐');
        } 
        $taocan = $setting_balance->taocan_detail;
        if(!isset($taocan[$selectIndex])){
            return $this->error('暂未配置充值套餐');
        }
        $couponIds = $taocan[$selectIndex]['coupon'];
        if(!empty($couponIds)){
            $time = date('Y-m-d',$this->request->time());
            $coupons = CouponModel::where([
                ['coupon_id','IN',$couponIds],
                ['valid_bg_time','<=',$time],
                ['valid_end_time','>=',$time],
                ['status','=',1],
                ['is_delete','=',0]
            ])->select()->toArray();
            $newCoupons = [];
            foreach($coupons as $val){
                $newCoupons[] = $val['coupon_id'];
            }
            $couponIds = $newCoupons;
        }

        $onceCards=$taocan[$selectIndex]['once_card'];
        if(!empty($onceCards)){
            $once_cards = OnceCardModel::where([
                ['once_card_id','IN',$onceCards],
                ['status','=',1],
                ['is_delete','=',0]
            ])->select()->toArray();
            $newOncecard = [];
            foreach($once_cards as $val){
                $newOncecard[] = $val['once_card_id'];
            }
            $onceCardIds = $newOncecard;
        }
        $datas = [
            'recharge_price' => $taocan[$selectIndex]['recharge_price'],
            'give_price'     => $taocan[$selectIndex]['give_price'],
            'coupons'        => $couponIds,
            'once_cards'     => $onceCardIds,
            'shop_id'        => $this->request->shopId,
            'member_id'      => $this->request->memberId,
            'is_paid'        => 0,   
        ];
        $BalanceOrderModel = new BalanceOrderModel();
        $BalanceOrderModel->save($datas);
        
        $PaymentBalanceHelper = new PaymentBalanceHelper($this->request->shopId);
        $params = $PaymentBalanceHelper->getPayment([
            'product_name' => '余额充值',
            'need_pay' => $datas['recharge_price'],
            'order_id' => $BalanceOrderModel->order_id,
            'open_id'  => $this->request->member->open_id 
        ]);
        return $this->success(['payment'=>$params]);
    }
    //充值返回
    public function balanceRespond(){
        $PaymentBalanceHelper = new PaymentBalanceHelper($this->request->shopId);
        $PaymentBalanceHelper->respond();
    }
    
    //拉起买单的微信支付
    public function getBuyPayment(){
         $order_id = $this->request->param('order_id/d');
        if(empty($order_id)){
            return $this->error('请选择要支付的订单');
        }
        if(null === $order = BuyOrderModel::find($order_id)){
            return $this->error('请选择要支付的订单');
        }
        if($order->shop_id != $this->request->shopId || $order->member_id != $this->request->memberId){
            return $this->error('请选择要支付的订单');
        }
        $PaymentBuyHelper = new PaymentBuyHelper($this->request->shopId);
        $needPay = round(($order->balance * 100 - $order->vip_balance * 100 - $order->coupon_money * 100 - $order->integral_balance * 100)/100,2);
        $params = $PaymentBuyHelper->getPayment([
            'product_name' => '在线买单',
            'need_pay' => $needPay,
            'order_id' => $order_id,
            'open_id'  => $this->request->member->open_id 
        ]);
        return $this->success(['payment'=>$params]);
    }
    
    //买单微信支付的回调
    public function buyRespond(){
       $PaymentBuyHelper = new PaymentBuyHelper($this->request->shopId);
       $PaymentBuyHelper->respond();
    }
    //囤货订单的支付
    public function getStoreMallPayment(){
        $order_id = $this->request->param('order_id/d');
        if(empty($order_id)){
            return $this->error('请选择要支付的订单');
        }
        if(null === $order = StoreOrderModel::find($order_id)){
            return $this->error('请选择要支付的订单');
        }
        if($order->status != 1 || $order->is_delete == 1){
            return $this->error('订单不在未支付状态');
        }
        $time = time() - 900;//订单15分钟
        if($order->add_time < $time){ //如果 订单超过15分钟则不能支付了
            return $this->error('订单支付已经超时了，不能继续支付');
        }
        if($order->shop_id != $this->request->shopId || $order->member_id != $this->request->memberId){
            return $this->error('订单不存在'); 
        }
        $PaymentStoreOrderHelper = new PaymentStoreOrderHelper($this->request->shopId);
        $needPay = $order->need_pay;
        $params = $PaymentStoreOrderHelper->getPayment([
            'product_name' => '商城囤货',
            'need_pay' => $needPay,
            'order_id' => $order_id,
            'open_id'  => $this->request->member->open_id 
        ]);
        return $this->success(['payment'=>$params]);
    }
    
    public function storeMallRespond(){
        $PaymentStoreOrderHelper = new PaymentStoreOrderHelper($this->request->shopId);
        $PaymentStoreOrderHelper->respond();
    }
    
    public function getPintuanMallPayment(){
        $order_id = $this->request->param('order_id/d');
        if(empty($order_id)){
            return $this->error('请选择要支付的订单');
        }
        if(null === $order = PinTuanOrderModel::find($order_id)){
            return $this->error('请选择要支付的订单');
        }
        if($order->status != 1){
            return $this->error('订单不在未支付状态');
        }
        $time = time() - 900;//订单15分钟
        if($order->add_time < $time){ //如果 订单超过15分钟则不能支付了
            return $this->error('订单支付已经超时了，不能继续支付');
        }
        if($order->shop_id != $this->request->shopId || $order->member_id != $this->request->memberId){
            return $this->error('订单不存在'); 
        }
        
        $PaymentPintuanOrderHelper = new PaymentPintuanOrderHelper($this->request->shopId);
        $needPay = $order->need_pay;
        $params = $PaymentPintuanOrderHelper->getPayment([
            'product_name' => '商城购物',
            'need_pay' => $needPay,
            'order_id' => $order_id,
            'open_id'  => $this->request->member->open_id 
        ]);
        return $this->success(['payment'=>$params]);
        
    }
    public function pintuanMallRespond(){
        $PaymentPintuanOrderHelper = new PaymentPintuanOrderHelper($this->request->shopId);
        $PaymentPintuanOrderHelper->respond();
    }
    
    
    //商城订单的支付
    public function getGoodsMallPayment(){
        $order_id = $this->request->param('order_id/d');
        if(empty($order_id)){
            return $this->error('请选择要支付的订单');
        }
        if(null === $order = GoodsOrderModel::find($order_id)){
            return $this->error('请选择要支付的订单');
        }
        if($order->status != 1 || $order->is_delete == 1){
            return $this->error('订单不在未支付状态');
        }
        $time = time() - 900;//订单15分钟
        if($order->add_time < $time){ //如果 订单超过15分钟则不能支付了
            return $this->error('订单支付已经超时了，不能继续支付');
        }
        if($order->shop_id != $this->request->shopId || $order->member_id != $this->request->memberId){
            return $this->error('订单不存在'); 
        }
        $PaymentGoodsOrderHelper = new PaymentGoodsOrderHelper($this->request->shopId);
        $needPay = round(($order->total_price * 100 + $order->freight * 100 - $order->coupon_money * 100 - $order->integral_balance * 100)/100,2);
        $params = $PaymentGoodsOrderHelper->getPayment([
            'product_name' => '商城购物',
            'need_pay' => $needPay,
            'order_id' => $order_id,
            'open_id'  => $this->request->member->open_id 
        ]);
        return $this->success(['payment'=>$params]);
         
    }
    //商城订单微信支付的回调
    public function goodsMallRespond(){
         $PaymentGoodsOrderHelper = new PaymentGoodsOrderHelper($this->request->shopId);
         $PaymentGoodsOrderHelper->respond();
    }


    public function getOncecardPayment(){
        $order_id = $this->request->param('once_card_order_id/d');
        if(empty($order_id)){
            return $this->error('请选择要支付的订单');
        }
        if(null === $order = OnceCardOrderModel::find($order_id)){
            return $this->error('请选择要支付的订单');
        }
        if($order->shop_id != $this->request->shopId || $order->member_id != $this->request->memberId){
            return $this->error('请选择要支付的订单');
        }
        $PaymentOncecardHelper = new PaymentOncecardHelper($this->request->shopId);
        $params = $PaymentOncecardHelper->getPayment([
            'product_name' => '次卡订单',
            'need_pay' => $order->need_pay,
            'order_id' => $order_id,
            'open_id'  => $this->request->member->open_id
        ]);
        return $this->success(['payment'=>$params]);
    }

    public function  oncecardRespond(){
        $PaymentOncecardHelper = new PaymentOncecardHelper($this->request->shopId);
        $PaymentOncecardHelper->respond();
    }

    public function getSnapupPayment(){
        $order_id = $this->request->param('snap_up_order_id/d');
        if(empty($order_id)){
            return $this->error('请选择要支付的订单');
        }
        if(null === $order = SnapUpOrderModel::find($order_id)){
            return $this->error('请选择要支付的订单');
        }
        if($order->shop_id != $this->request->shopId || $order->member_id != $this->request->memberId){
            return $this->error('请选择要支付的订单');
        }
        $PaymentSnapUpHelper = new PaymentSnapupHelper($this->request->shopId);
        $params = $PaymentSnapUpHelper->getPayment([
            'product_name' => '抢购订单',
            'need_pay' => $order->need_pay,
            'order_id' => $order_id,
            'open_id'  => $this->request->member->open_id
        ]);
        return $this->success(['payment'=>$params]);
    }
    public function snapupRespond(){
        $PaymentSnapUpHelper = new PaymentSnapupHelper($this->request->shopId);
        $PaymentSnapUpHelper->respond();
    }
    
    
    
    
}
